package com.hyphenate.easeui.model.dao;

import android.content.Context;

/**
 * Simple to Introduction
 *
 * @ProjectName: com.hyphenate.easeui.model.dao
 * @Description: [一句话描述该类的功能]
 * @Author: Administrator
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/9/22 15:51
 * @Version: 1.0.0
 */
public class UserIconHelper {

    private static DaoMaster mDaoMaster;
    private static DaoSession mDaoSession;
    public static final String DATABASE_NAME = "user_icon.db";


    public static final String KEY_NICK_NAME = "nickname";
    public static final String KEY_AVATAR = "avatarURLPath";


    public static DaoMaster getDaoMaster(Context context) {
        if (mDaoMaster == null) {
            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
            mDaoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return mDaoMaster;
    }


    /**
     * 取得DaoSession
     *
     * @param context
     * @return
     */
    public static DaoSession getDaoSession(Context context) {
        if (mDaoSession == null) {
            if (mDaoMaster == null) {
                mDaoMaster = getDaoMaster(context);
            }
            mDaoSession = mDaoMaster.newSession();
        }
        return mDaoSession;
    }



    public static UserIconMap getUserIconBy(Context context, String username) {
        return getDaoSession(context).getUserIconMapDao().load(username);
    }

    public static void setUserIcon(Context context, UserIconMap map) {
        getDaoSession(context).getUserIconMapDao().insertOrReplace(map);
    }

    public static void setUserIcon(Context context, String username, String nickname, String avatar) {
        UserIconMap map = new UserIconMap();
        map.setAvatar(avatar);
        map.setUserName(username);
        map.setNickName(nickname);
        setUserIcon(context, map);
    }


}
